<?php
// {{ ansible_managed }}

# Name of the environment for this deploy
$m_environment="{{ env }}";

$wiki_app_fqdn = '{{ wiki_app_fqdn }}';

### DIRECTORY STRUCTURE ###

# meza install location
$m_install = "{{ m_install }}";
$m_meza = "{{ m_meza }}";

$m_config_core = "{{ m_config_core }}";
$m_scripts = "{{ m_scripts }}";
$m_test = "{{ m_test }}";
$m_tmp = "{{ m_tmp }}";

# data dir
$m_meza_data = "{{ m_meza_data }}";
$m_uploads_dir='{{ m_uploads_dir }}';
$m_backups="{{ m_backups }}";
$allow_backup_downloads = '{{ allow_backup_downloads }}';

# Things deployed by ansible
$m_deploy = '{{ m_deploy }}';

# webserver variables
$m_htdocs = "{{ m_htdocs }}";
$m_mediawiki = "{{ m_mediawiki }}";

# Installation configuration
$db_master = "{{ groups['db_master'][0] }}";
$wiki_app_db_user_name = '{{ wiki_app_db_user.name }}';
$wiki_app_db_user_pass = '{{ wiki_app_db_user.password }}';


$m_language = "{{ m_language }}";

#
# Logging
#
$m_logging_db_name = 'meza_server_log';
$m_logging_db_host = 'localhost';


{% if server_admins is defined %}
#
# Server admins only
#
$server_admins = array();
{% for sadmin in server_admins %}
$server_admins[] = '{{ sadmin }}';
{% endfor %}
{% endif %}

{% if saml_public is defined %}
#
# SAML
#
$saml_idp_username_attr = '{{ saml_public.idp_username_attr }}';

{% endif %}

#
# Downloading backups
#
{% if backups_environment is defined %}
# Allow downloading backups from this environment
$backups_environment = '{{ backups_environment }}';
{% endif %}

{% if wiki_backup_downloaders is defined %}
# Users allowed to download specific wikis
$wiki_backup_downloaders = array();
{% for wiki, users in wiki_backup_downloaders.items() %}
$wiki_backup_downloaders['{{ wiki }}'] = array(
	{% for user in users %}
	'{{ user }}',
	{% endfor %}
);
{% endfor %}
{% endif %}

{% if all_backup_downloaders is defined %}
// users allowed ot download from all wikis
$all_backup_downloaders = array(
	{% for user in all_backup_downloaders %}
	'{{ user }}',
	{% endfor %}
);
{% endif %}


#
# DEPLOY VARS included in the following order, with each superceding the previous
#   1. public
#   2. env-specific
#   3. secret
#   4. FIXME: deprecated deploy_vars, use secret_deploy_vars instead
#
{% if public_deploy_vars is defined and public_deploy_vars is iterable %}
#
# CUSTOM __public__ DEPLOY VARIABLES
# These should only come from public.yml
#
{% for key, value in public_deploy_vars.items() %}

{% if value is number -%}
	${{ key }} = {{ value }};

{%- elif value is string -%}
	${{ key }} = '{{ value }}';

{%- elif value is iterable -%}
	${{ key }} = [];

	{%- if value is mapping -%}
		{%- for subkey, subvalue in value.items() %}

			{% if subvalue is number -%}
				${{ key }}['{{ subkey }}'] = {{ subvalue }};
			{%- else -%}
				${{ key }}['{{ subkey }}'] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- else -%}
		{%- for subvalue in value %}

			{% if subvalue is number -%}
				${{ key }}[] = {{ subvalue }};
			{%- else -%}
				${{ key }}[] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- endif -%}

{%- else -%}
	${{ key }} = '{{ value }}';

{%- endif %}

{% endfor -%}
{%- endif %}



{% if env_deploy_vars is defined and env_deploy_vars is iterable %}
#
# CUSTOM __{{ env }} environment__ DEPLOY VARIABLES
# These should only come from env/{{ env }}.yml
#
{% for key, value in env_deploy_vars.items() %}

{% if value is number -%}
	${{ key }} = {{ value }};

{%- elif value is string -%}
	${{ key }} = '{{ value }}';

{%- elif value is iterable -%}
	${{ key }} = [];

	{%- if value is mapping -%}
		{%- for subkey, subvalue in value.items() %}

			{% if subvalue is number -%}
				${{ key }}['{{ subkey }}'] = {{ subvalue }};
			{%- else -%}
				${{ key }}['{{ subkey }}'] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- else -%}
		{%- for subvalue in value %}

			{% if subvalue is number -%}
				${{ key }}[] = {{ subvalue }};
			{%- else -%}
				${{ key }}[] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- endif -%}

{%- else -%}
	${{ key }} = '{{ value }}';

{%- endif %}

{% endfor -%}
{%- endif %}


{% if secret_deploy_vars is defined and secret_deploy_vars is iterable %}
#
# CUSTOM __secret__ DEPLOY VARIABLES
# These should only come from secret.yml
#
{% for key, value in secret_deploy_vars.items() %}

{% if value is number -%}
	${{ key }} = {{ value }};

{%- elif value is string -%}
	${{ key }} = '{{ value }}';

{%- elif value is iterable -%}
	${{ key }} = [];

	{%- if value is mapping -%}
		{%- for subkey, subvalue in value.items() %}

			{% if subvalue is number -%}
				${{ key }}['{{ subkey }}'] = {{ subvalue }};
			{%- else -%}
				${{ key }}['{{ subkey }}'] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- else -%}
		{%- for subvalue in value %}

			{% if subvalue is number -%}
				${{ key }}[] = {{ subvalue }};
			{%- else -%}
				${{ key }}[] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- endif -%}

{%- else -%}
	${{ key }} = '{{ value }}';

{%- endif %}

{% endfor -%}
{%- endif %}



{% if deploy_vars is defined and deploy_vars is iterable %}
#
# __DEPRECATED__ CUSTOM DEPLOY VARIABLES
#
# FIXME: remove deploy_vars in lieu of secret_deploy_vars when production wikis
#        are updated.
#
{% for key, value in deploy_vars.items() %}

{% if value is number -%}
	${{ key }} = {{ value }};

{%- elif value is string -%}
	${{ key }} = '{{ value }}';

{%- elif value is iterable -%}
	${{ key }} = [];

	{%- if value is mapping -%}
		{%- for subkey, subvalue in value.items() %}

			{% if subvalue is number -%}
				${{ key }}['{{ subkey }}'] = {{ subvalue }};
			{%- else -%}
				${{ key }}['{{ subkey }}'] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- else -%}
		{%- for subvalue in value %}

			{% if subvalue is number -%}
				${{ key }}[] = {{ subvalue }};
			{%- else -%}
				${{ key }}[] = '{{ subvalue }}';
			{%- endif -%}

		{% endfor -%}

	{%- endif -%}

{%- else -%}
	${{ key }} = '{{ value }}';

{%- endif %}

{% endfor -%}
{%- endif %}



#
# SERVER PERFORMANCE
#
{% if slack_channel_server_performance is defined %}
# Slack channel to send server performance info. If undefined, will use webhook default
$slack_channel_server_performance="{{ slack_channel_server_performance }}";
{% endif %}

{% if slack_webhook_token_server_performance is defined %}
# Slack incoming webhook token for sending server performance info
$slack_webhook_token_server_performance="{{ slack_webhook_token_server_performance }}";
{% endif %}

{% if slack_username_server_performance is defined %}
# Slack username to display for webhook
$slack_username_server_performance="{{ slack_username_server_performance }}";
{% endif %}


#
# DISK USAGE
#
{% if disk_space_usage_mount_name is defined %}
$disk_space_usage_mount_name="{{ disk_space_usage_mount_name }}";
{% endif %}

# A short name used in description when sending Slack message
$disk_space_usage_mount_short_name="{{ disk_space_usage_mount_short_name | default('Disk') }}";

{% if slack_channel_disk_usage is defined %}
# Slack channel to send disk usage info. If undefined, will use webhook default
$slack_channel_disk_usage="{{ slack_channel_disk_usage }}";
{% endif %}

{% if slack_webhook_token_disk_usage is defined %}
# Slack incoming webhook token for sending disk usage info
$slack_webhook_token_disk_usage="{{ slack_webhook_token_disk_usage }}";
{% endif %}

{% if slack_username_disk_usage is defined %}
# Slack username to display for webhook
$slack_username_disk_usage="{{ slack_username_disk_usage }}";
{% endif %}


#
# Auto-deployer
#
{% if secret_config_repo is defined and secret_config_repo.repo is defined %}
# Info about local public config
$secret_config_repo_repo="{{ secret_config_repo.repo }}";
$secret_config_repo_version="{{ secret_config_repo.version | default('master') }}";
$secret_config_repo_force="{{ secret_config_repo.force | default(false) | bool }}";
{% endif %}

{% if local_config_repo is defined and local_config_repo.repo is defined %}
# Info about local public config
$local_config_repo_repo="{{ local_config_repo.repo }}";
$local_config_repo_version="{{ local_config_repo.version | default('master') }}";
$local_config_repo_force="{{ local_config_repo.force | default(false) | bool }}";
{% endif %}

{% if enforce_meza_version is defined %}
# What Meza version to track
$enforce_meza_version="{{ enforce_meza_version }}";
{% endif %}

{% if autodeployer is defined %}
{% if autodeployer.slack_channel is defined %}
$autodeployer_slack_channel="{{ autodeployer.slack_channel }}";
{% endif %}

{% if autodeployer.slack_token is defined %}
$autodeployer_slack_token="{{ autodeployer.slack_token }}";
{% endif %}

{% if autodeployer.slack_username is defined %}
$autodeployer_slack_username="{{ autodeployer.slack_username }}";
{% endif %}

{% if autodeployer.slack_icon_url is defined %}
$autodeployer_slack_icon_url="{{ autodeployer.slack_icon_url }}";
{% endif %}
{% endif %}


